/**
 * Company : SIANGKLOM.
 * Created By : Chatree Siangklom
 * Created On : Oct 1, 2013  2:31:16 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.encryption
 * File Name : AESUrlencrp.java
 * TODO
 */
package com.tis.encryption;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Oct 1, 2013 : 2:31:16 PM 
 * CreateBy=Chatree Siangklom
 * TODO
 */
public class AESUrlencrp {

	private static final String ALGO = "AES";
	private static final byte[] keyValue = new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't', 'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
	
	
	
	/**
	 * Date =Sep 26, 2013 : 1:53:11 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 * @param Data
	 * @return
	 * @throws Exception
	 */
	public static String encrypt(String Data) throws Exception {
	
	        String encryptedValue = encrypt_B(encrypt_A(Data));
	        return ReplaceSpacialchar(encryptedValue);
	}
	
	/**
	 * Date =Oct 1, 2013 : 2:35:11 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 * @param Data
	 * @return
	 * @throws Exception
	 */
	private static String encrypt_A(String Data) throws Exception {
		Key key = generateKey();
		Cipher c = Cipher.getInstance(ALGO);
		c.init(Cipher.ENCRYPT_MODE, key);
		byte[] encVal = c.doFinal(Data.getBytes());
		String encryptedValue = new BASE64Encoder().encode(encVal);
		return encryptedValue;
	}
	
	/**
	 * Date =Oct 1, 2013 : 2:34:59 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 * @param Data
	 * @return
	 * @throws Exception
	 */
	private static String encrypt_B(String Data) throws Exception {
		Key key = generateKey();
		Cipher c = Cipher.getInstance(ALGO);
		c.init(Cipher.ENCRYPT_MODE, key);
		byte[] encVal = c.doFinal(Data.getBytes());
		String encryptedValue = new BASE64Encoder().encode(encVal);
		return encryptedValue;
	}

	/**
	 * 
	 * Date =Sep 26, 2013 : 1:53:18 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 * @param encryptedData
	 * @return
	 * @throws Exception
	 */
	public static String decrypt(String encryptedData) throws Exception {
        String decryptedValue = decrypt_B(decrypt_A(RedoReplaceSpacialchar(encryptedData)));
        return decryptedValue;
    }
	
	/**
	 * Date =Oct 1, 2013 : 2:34:39 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 * @param encryptedData
	 * @return
	 * @throws Exception
	 */
	private static String decrypt_A(String encryptedData) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }
	
	/**
	 * Date =Oct 1, 2013 : 2:34:42 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 * @param encryptedData
	 * @return
	 * @throws Exception
	 */
	private static String decrypt_B(String encryptedData) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }
    
    /**
     * 
     * Date =Sep 26, 2013 : 1:52:32 PM 
     * CreateBy=Chatree Siangklom
     * TODO
     * @return
     * @throws Exception
     */
    private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGO);
        return key;
    }
    
    private static String [] spacial_charString = { ":" , "@" , "&" , "=" , "+" , "$" , "," , ";" , "/" , "?" , "'" , "-" , "%" };
	private static String [] spacial_newcharString = {"{||1||}" , "{||2||}" , "{||3||}" , "{||4||}" , "{||5||}" , "{||6||}" , "{||7||}" , "{||8||}" , "{||9||}" , "{||10||}", "{||11||}", "{||12||}", "{||13||}" };
	
	public static String ReplaceSpacialchar(String data) throws Exception {
		
		for(int i = 0; i < spacial_charString.length; i ++){
			System.out.println(spacial_charString[i] + "=" + data.contains(spacial_charString[i]));
			
			if(data.contains(spacial_charString[i])){
				data = data.replace(spacial_charString[i], spacial_newcharString[i]);
			}
		}
		return data;
	}
	
	public static String RedoReplaceSpacialchar(String data) throws Exception {
		
		for(int i = 0; i < spacial_newcharString.length; i ++){
			System.out.println(spacial_newcharString[i] + "=" + data.contains(spacial_newcharString[i]));
			
			if(data.contains(spacial_newcharString[i])){
				data = data.replace(spacial_newcharString[i], spacial_charString[i]);
			}
		}
		return data;
	}

    public static void main(String [] args) {
    	try {
    		
    		String interfacekey = "YVNHHSgFOGH0VS70UT3d0w{||4||}{||4||}*qfpwb9D58wdxK7G1{||9||}Hu28Q{||4||}{||4||}";
    		String [] strVal = interfacekey.split("\\*");
			String user = strVal[0];
			String password = strVal[1];
    		
			System.out.println("user=" + user);
			System.out.println("password=" + password);
			
    		String Data = "YVNHHSgFOGH0VS70UT3d0w{||4||}{||4||}";
    		String encryptedData = AESUrlencrp.encrypt(Data);
    		String decryptData = AESUrlencrp.decrypt(encryptedData);
    		System.out.println("encryptedData =" + encryptedData);
    		System.out.println("decryptData =" + decryptData);
    		
    		
    	}catch(Exception e) {
    		e.printStackTrace();
    	}
    }

}
